--#include "data\config\activityconfig\DailyActivity\NightFightingCfg.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理

--[[
local Info =
{
	[1] = 0,		--0没到报名时间,1可以报名了,2已经开始了,3 30秒倒计时状态
	[2] = 0,		--当前回合
	[3] = 0,		--当前回合开始时间
	[4] = 0,		--boss的handle
}
]]

function NightFightingEnterScene(sysarg, reliveType, bResult)
	local rand = System.getRandomNumber(#NightFightingCfg.EnterPos) + 1		
	if reliveType and reliveType == 1 then
	elseif reliveType and reliveType == 2 then
		Actor.enterScene(sysarg,unpack(NightFightingCfg.EnterPos[rand]))
	else
		NightFightingSetCamp(sysarg, bResult)
		Actor.enterScene(sysarg,unpack(NightFightingCfg.EnterPos[rand]))
		if(bResult == nil)then	
			-- 6 参加夜战皇城
			local sVar = Actor.getStaticVar(sysarg)
			if not sVar.days then sVar.days = {} end
			if not sVar.days.nightFightingState then
				Actor.triggerAchieveEvent(sysarg, 44, 6)
				sVar.days.nightFightingState = 1
			end	

			local nActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID ) 
			local nVocation = Actor.getIntProperty(sysarg,PROP_ACTOR_VOCATION)
			local nLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)	
			gRankMgr_SetRankEx(nActorId, RANK_DEFINE_NIGHTFIGHTING, 1, nVocation)
			gRankMgr_SetRankEx(nActorId, RANK_DEFINE_NIGHTFIGHTING, 2, nLevel)
		end	
	end
	if reliveType then
		local maxHp = Actor.getIntProperty(sysarg, PROP_CREATURE_MAXHP)
		Actor.setUIntProperty(sysarg, PROP_CREATURE_HP, maxHp)
	end
	OpenNightFightingRightPanel(sysarg)
end	

--获得奖励配置
function getNightFightingCount()
	local sysVar = System.getDyanmicVar()
	if(sysVar.NightFightingStartTime == nil)then
		sysVar.NightFightingStartTime = 0 
	end
	local nstarTime = System.getCurrMiniTime() - sysVar.NightFightingStartTime
	for k,v in pairs(NightFightingCfg.awardMultiple)do 
		if(nstarTime <= v)then
			return k
		end
	end
	return 1
end

--一段时间对场景活着的玩家发经验
function NightFightingTimer()
	local nSceneId = NightFightingCfg.EnterPos[1][1]
	local playerListAll = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表
	if(playerListAll == nil or #playerListAll == 0 )then	
		return false
	end
	local playerList = {}
	for k,v in pairs(playerListAll)do		--排除死了的玩家
		if (Actor.isDeath( v ) == false) then
			table.insert(playerList,v)
		end	
	end
	if(#playerList == 0)then
		return false
	end
	local cfg = NightFightingCfg						
	local index = getNightFightingCount()
	local awards = {}
	local AddScore = 0
	for k,v in pairs(cfg.timeAward[index] or {} )do
		if(v.type ~= qatNightFighting)then	
			table.insert(awards, v)
		else
			AddScore = v.count
		end
	end
	local logId, logStr = LogIds.NightFightingLogId, Lang.Activity.NightFighting	
	local updateState = false
	for k,sysarg in pairs(playerList)do
		if Actor.hasMapAreaAttri(sysarg, aaPaoDianAddExpArea) then
			Awards.Give(sysarg, awards, logId, logStr)
			local nActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID )
			if(0 < AddScore)then
				gRankMgr_AddValue(nActorId, RANK_DEFINE_NIGHTFIGHTING, AddScore)
				Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.NightFighting001, AddScore), ttTipmsgWindow)	
				--NightFightingGetRank(sysarg, 0)
				updateState = true
			end	
		end	
	end
	if updateState then
		ChangeNightFightingRightPanelAll()
	end
end
  
--活动开始
function NightFightingStar()	-- @DebugLua 1003 13 1
	local cfg = NightFightingCfg
	--加经验
	gRankMgr_Clear(RANK_DEFINE_NIGHTFIGHTING)
	Actor.unregScriptCallback( System.getGlobalNpc(), "NightFightingTimer")	
	Actor.unregScriptCallback( System.getGlobalNpc(), "NightFightingSeconds")	
	Actor.regScriptTimer(System.getGlobalNpc(), 0, 0, cfg.timeSpace * 1000, cfg.timeCount, "NightFightingTimer") 
	local nTime = NightFightingCfg.OneTime - NightFightingCfg.SpaceTime
	Actor.regScriptTimer(System.getGlobalNpc(), 0, nTime*1000, nTime*1000, 1, "NightFightingSeconds")	--倒计时
	
	local sysVar = System.getDyanmicVar()
	sysVar.NightFightingDataSys = nil
	sysVar.NightFightingStartTime = System.getCurrMiniTime()
	sysVar.NightFightingStartFlag = 1


	OpenNightFightingRightPanelAll()

	System.broadcastTipmsg(Lang.ScriptTips.NightFighting012,ttScreenCenter + ttChatWindow)
end

--二轮之间倒计时
function NightFightingSeconds()
	--BaseFuc_Print("Enter NightFightingSeconds")
	local NightFightingSys = {0,0,0,0}
	local sysVar = System.getDyanmicVar()
	if(sysVar.NightFightingDataSys ~= nil)then
		NightFightingSys = BaseFuc_unserialize(sysVar.NightFightingDataSys)	
	end		
	NightFightingSys[1] = 3
	sysVar.NightFightingDataSys = BaseFuc_serialize(NightFightingSys)	
	local nSceneId = NightFightingCfg.EnterPos[1][1]
	local hScene = Fuben.getSceneHandleById(nSceneId, 0)	
	if(NightFightingCfg.count <= NightFightingSys[2])then	--活动完了
		--Fuben.sendMsgToScene(hScene,Lang.ScriptTips.NightFighting004, ttChatWindow + ttScreenCenter)
		NightFightingEnd()
		return 	
	end	
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  
	if playerList ~= nil then
		local nLen = #playerList
		for i = 1, nLen do	
			local player = playerList[i]
			Actor.sendCountdown(player, NightFightingCfg.SpaceTime , 0 , 2)	--30秒倒计时
			OpenBoardPanel( player, enBoardPanelStyle_Default, Lang.ScriptTips.NightFighting003 )		
			--Actor.SendShortCutMsg(player, Lang.ScriptTips.NightFighting003)			
		end
	end
	
	Actor.unregScriptCallback( System.getGlobalNpc(), "NightFightingFiveMinEndOne")	
	Actor.regScriptTimer(System.getGlobalNpc(), 0, NightFightingCfg.SpaceTime*1000, NightFightingCfg.SpaceTime*1000, 1, "NightFightingFiveMinEndOne")	
end

--活动5分钟后结束一轮开始下一轮
function NightFightingFiveMinEndOne()
	NightFightingSetzyVal()	
	local nSceneId = NightFightingCfg.EnterPos[1][1]
	local playerList = LuaHelp.getSceneActorListById(nSceneId)  
	if(playerList == nil)then
		return	
	end
	local nLen = #playerList
	for i = 1, nLen do	
		local player = playerList[i]
		CloseBoardPanel( player )
	end
end
	
--活动结束
function NightFightingEnd()	-- @DebugLua 1003 13 2
	local sysVar = System.getDyanmicVar()
	if sysVar.NightFightingStartFlag then
		NightFightingAward()

		local nSceneId = NightFightingCfg.EnterPos[1][1]
		System.broadcastTipmsg(Lang.ScriptTips.NightFighting013,ttScreenCenter + ttChatWindow)

		Actor.unregScriptCallback( System.getGlobalNpc(), "NightFightingTimer")		
		
		sysVar.NightFightingStartFlag = nil

		SendAchieveNightFightingRankEvent()

		local playerList = LuaHelp.getSceneActorListById(nSceneId)  --获取在线玩家对象列表	
		if(playerList == nil)then
			return
		end
		for k,v in pairs(playerList)do	--踢出场景
			Actor.enterScene(v,unpack(NightFightingCfg.ExitPos))	
			Actor.setPkMode(v, fpPeaceful)	--PK模式
			Actor.setCampId(v, 0)
			Actor.updateActorName(v)
			CloseActivityRightPanel(v)			--关闭活动面板
			NightFightingGetRank(v, 1)	
			SendActivityLog(v, ACTIVITY_DEFINE_NIFHTFIFHTING, ActLogStatus_Complete)
		end
	end
end

function NightFightingLeaveActivityHandler(sysarg)
	--NightFightingGetRank(sysarg, 0)
	local sysVar = System.getDyanmicVar()

	if sysVar.NightFightingStartFlag then
		local actorId = Actor.getActorId(sysarg)

		local myIdx = 0
		local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
		local rankingPtr = Ranking.getRanking( rankName )
		if rankingPtr then
			local item = Ranking.getItemPtrFromId( rankingPtr, actorId)
			if item then
				local myScore = Ranking.getPoint(item) or 0
				if myScore < NightFightingCfg.rankLimit then			--未上榜
					myIdx = 0
				else
					myIdx = Ranking.getIndexFromPtr(item) + 1
				end
			end	
		end

		gRankMgr_SetRank(actorId, RANK_DEFINE_NIGHTFIGHTING, 0)	

		if myIdx > 0 then
			ChangeNightFightingRightPanelAll(sysarg)
		end
	end
	Actor.setCampId(sysarg, 0)
	Actor.updateActorName(sysarg)
	Actor.setPkMode(sysarg, fpPeaceful)	
	CloseActivityRightPanel(sysarg)	
end

--离开活动场景
function NightFightingLeaveActivity(sysarg)
	Actor.enterScene(sysarg,unpack(NightFightingCfg.ExitPos))
end

--划分阵营
function NightFightingSetCamp(sysarg, bResult)
	local sysVar = System.getDyanmicVar()	
	local zyVal = 1
	if(sysVar.NightFightingZyVal ~= nil)then
		zyVal = sysVar.NightFightingZyVal
	end
	zyVal = zyVal
	if(zyVal == 1)then
		zyVal = 2
	elseif(zyVal == 2)then	
		zyVal = 1
	end
	--我的数据
	local saveState = true
	if(bResult ~= nil and bResult == 1 ) then	--回合结束强制换阵营
		zyVal = System.getRandomNumber(2)+1
	else
		if(bResult == nil)then
			local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
			local ranking = Ranking.getRanking( rankName )
			if (ranking ~= nil) then
				local myId = Actor.getActorId(sysarg)
				local item = Ranking.getItemPtrFromId( ranking, myId)
				if item then
					local playerCampVal = Ranking.getSub(item, 3) 
					if playerCampVal and ( tonumber(playerCampVal) == 1 or tonumber(playerCampVal) == 2 ) then
						zyVal = tonumber(playerCampVal)
						saveState = false
					end
				end	
			end	
		end		
	end	
	
	local nActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID )	
	gRankMgr_SetRankEx(nActorId, RANK_DEFINE_NIGHTFIGHTING, 3, zyVal)	
	--print("nActorId ="..nActorId)
	--print("zyVal ="..zyVal)
	Actor.setCampId(sysarg, zyVal)
	if(bResult == nil) and saveState then
		Actor.updateActorName(sysarg)
	end
	Actor.setPkMode(sysarg, fpZY)
	if saveState then
		--print("saveState zyVal ="..zyVal)
		sysVar.NightFightingZyVal = zyVal
	end
	--print("zyVal",zyVal)
end

--下发面板单独处理
function NightFightingAotoTiem(playerList, NightFighting)	
	local nLen = #playerList	
	for i = 1, nLen do		
		local player = playerList[i]
		NightFightingGetRank(player)			--下发面板
	end
end

--划分阵营
function NightFightingSetzyVal()
	--BaseFuc_Print("Enter NightFightingSetzyVal")
	local sysVar = System.getDyanmicVar()
	local NightFightingSys = {0,0,0,0}
	if(sysVar.NightFightingDataSys ~= nil)then
		NightFightingSys = BaseFuc_unserialize(sysVar.NightFightingDataSys)	
	end	
	NightFightingSys[1] = 2
	if(NightFightingCfg.count <= NightFightingSys[2])then	--活动轮完了
		return 	
	end
	NightFightingSys[2] = NightFightingSys[2] + 1
	NightFightingSys[3] = System.getCurrMiniTime()	
	local cfg = NightFightingCfg.Enter
	local NightFighting = {}
	if(sysVar.NightFightingActor ~= nil)then
		NightFighting = BaseFuc_unserialize(sysVar.NightFightingActor)	
	end		
	ClearNightFightingRankPlayerCamp()
	local playerList = LuaHelp.getSceneActorListById(NightFightingCfg.EnterPos[1][1])  
	if playerList ~= nil then
		local nLen = #playerList	
		local nLenTable = GetNightFightingRandomNumber(nLen)
		for i = 1, nLen do		
			local player = playerList[nLenTable[i]]
			if(i% 2 ==0)then
				NightFightingEnterScene(player, nil, 0)	
			else
				NightFightingEnterScene(player, nil, 1)	
			end
		end
		for i = 1, nLen do	
			local player = playerList[i]
			Actor.updateActorName(player)
		end		
	end
	sysVar.NightFightingDataSys = BaseFuc_serialize(NightFightingSys)
	local nTime = NightFightingCfg.OneTime - NightFightingCfg.SpaceTime
	Actor.regScriptTimer(System.getGlobalNpc(), 0, nTime*1000, nTime*1000, 1, "NightFightingSeconds")	--倒计时
	sysVar.NightFightingActor = BaseFuc_serialize(NightFighting)	
	--NightFightingAotoTiem(playerList, NightFighting)	--因为要下发最新的数据,所以保存了再发
	ChangeNightFightingRightPanelAll()
end

--杀人加分 sysarg = 被杀者
function NightFightingActorDeath( sysarg, killerPtr )
	--BaseFuc_Print("Enter NightFightingActorDeath")
	if(sysarg == nil or killerPtr == nil)then
		return
	end
	if(NightFightingCfg.EnterPos[1][1] ~= Actor.getSceneId(sysarg))then			--不是在阵营战场死的	
		return
	end

	if Actor.isMonster(killerPtr) then
		return
	end
	local myCamp = Actor.getIntProperty(sysarg, PROP_ACTOR_ZY)
	local killerCamp = Actor.getIntProperty(killerPtr, PROP_ACTOR_ZY)
	if myCamp == 0 or killerCamp == 0 or myCamp == killerCamp then
		return
	end
	local myLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
	local YouLevel = Actor.getIntProperty(killerPtr, PROP_CREATURE_LEVEL)
	if(myLevel + NightFightingCfg.GapLevel < YouLevel)then						--等级差距太大没有奖励
		--return
	end	
	local dieActorId = Actor.getIntProperty( sysarg, PROP_ENTITY_ID )
	local nActorId = Actor.getIntProperty( killerPtr, PROP_ENTITY_ID ) 
	local strName = Actor.getName(killerPtr)
	local dieScore = gRankMgr_GetValue(dieActorId, RANK_DEFINE_NIGHTFIGHTING) or 0
	local Score = gRankMgr_GetValue(nActorId, RANK_DEFINE_NIGHTFIGHTING) or 0
	local AddScore = math.modf(dieScore * NightFightingCfg.nKillScore / 100)
	if(NightFightingCfg.nMinScore < dieScore)then
		dieScore = dieScore - AddScore
		local loseScore = AddScore
		if(dieScore < NightFightingCfg.nMinScore)then		--死亡者的最低分数
			loseScore = loseScore - NightFightingCfg.nMinScore + dieScore
			dieScore = NightFightingCfg.nMinScore
		end	
		if(0 < loseScore)then
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.NightFighting007,loseScore), ttTipmsgWindow)
		end
	end
	
	if(AddScore < NightFightingCfg.nMinAddScore)then		--杀人者最低加分
		AddScore = NightFightingCfg.nMinAddScore
	end

	local myBeforeIdx = 0
	local dieBeforeIdx = 0
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < NightFightingCfg.rankLimit then			--未上榜
				myBeforeIdx = 0
			else
				myBeforeIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
		local item = Ranking.getItemPtrFromId( rankingPtr, dieActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < NightFightingCfg.rankLimit then			--未上榜
				dieBeforeIdx = 0
			else
				dieBeforeIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	

	
	
	gRankMgr_AddValue(nActorId, RANK_DEFINE_NIGHTFIGHTING, AddScore)
	gRankMgr_SetRank(dieActorId, RANK_DEFINE_NIGHTFIGHTING, dieScore)	


	local myCurIdx = 0
	local dieCurIdx = 0
	if rankingPtr then
		local item = Ranking.getItemPtrFromId( rankingPtr, nActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < NightFightingCfg.rankLimit then			--未上榜
				myCurIdx = 0
			else
				myCurIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
		local item = Ranking.getItemPtrFromId( rankingPtr, dieActorId)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < NightFightingCfg.rankLimit then			--未上榜
				dieCurIdx = 0
			else
				dieCurIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	--NightFightingGetRank(killerPtr)	--刷新面板
	--NightFightingGetRank(sysarg)	

	if dieCurIdx ~= dieBeforeIdx  or myBeforeIdx ~= myCurIdx then
		ChangeNightFightingRightPanelAll()
	else
		ChangeNightFightingRightPanel(killerPtr)
		ChangeNightFightingRightPanel(sysarg)
	end
	Actor.sendTipmsg(killerPtr, string.format(Lang.ScriptTips.NightFighting001,AddScore), ttTipmsgWindow)
	--BaseFuc_Print("0000000000000",ttTipmsgWindow)
end

--前端请求获得排行榜
function NightFightingGetRank(sysarg, reslut)
	--BaseFuc_Print("Entrer NightFightingGetRank")	
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local rankingPtr = Ranking.getRanking( rankName )
	if (rankingPtr == nil) then
		return
	end
	local num = NightFightingCfg.RankCount
	local itemNum = Ranking.getRankItemCount(rankingPtr)
	if num < Ranking.getRankItemCount(rankingPtr) then
		itemNum = num
	end
	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetNightFightRank)
	if (pack == nil) then
		return
	end
	local rankLimit = NightFightingCfg.rankLimit
	--我的数据
	local myId = Actor.getActorId(sysarg)
	local myScore 	= 0
	local myIdx 	= 0
	local item = Ranking.getItemPtrFromId( rankingPtr, myId)
	if item then
		myScore = Ranking.getPoint(item) or 0
		if myScore < rankLimit then			--未上榜
			myIdx = 0
		else
			myIdx = Ranking.getIndexFromPtr(item) + 1
		end
	end	

	--print("myIdx = "..myIdx)
	--print("myScore = "..myScore)
	
	local sysVar = System.getDyanmicVar()
	if(sysVar.NightFightingStartTime == nil)then
		sysVar.NightFightingStartTime = System.getCurrMiniTime()
	end
	local nTime = System.getCurrMiniTime() - sysVar.NightFightingStartTime	
	local nTime = NightFightingCfg.Time - nTime
	if(nTime < 0)then
		nTime = 0
	end
	DataPack.writeWord(pack, nTime)
	DataPack.writeWord(pack, myIdx)	
	local sendNum = true
	local sendDataNum 
	--BaseFuc_Print("itemNum",itemNum)
	for idx=itemNum,1, -1 do
		local rankItem = Ranking.getItemFromIndex(rankingPtr, idx-1)
		if rankItem then
			local value = Ranking.getPoint(rankItem)
			if value >= rankLimit then
				if sendNum then
					if idx > NightFightingCfg.FinialRankShowNum then
						sendDataNum = NightFightingCfg.FinialRankShowNum
					else
						sendDataNum = idx
					end
					DataPack.writeWord(pack, sendDataNum)
					sendNum = false
				end
				if idx <= sendDataNum then
					local name = Ranking.getSub(rankItem, 0)
					DataPack.writeString(pack, name)			
					DataPack.writeUInt(pack, value)
					DataPack.writeByte(pack, tonumber(Ranking.getSub(rankItem, 1)) )
					DataPack.writeWord(pack, tonumber(Ranking.getSub(rankItem, 2)) )

					--print("idx = "..idx)
					--print("name = "..name)
					--print("value = "..value)
				end
			end

			--BaseFuc_Print("name,value",name,value)
		end
	end

	if sendNum then
		DataPack.writeWord(pack, 0)
	end

	if(reslut == nil)then
		reslut = 0
	end
	DataPack.writeByte(pack, reslut)
	--print("myScore =" ..myScore)
	DataPack.writeWord(pack, myScore)
	local fightingRewards = NightFightingGetAward(sysarg, myIdx)
--	PrintAwardsTable(fightingRewards)
	DataPack.writeByte(pack, #fightingRewards)
	for k,v in pairs(fightingRewards)do
		DataPack.writeByte(pack, v.type or 0)
		DataPack.writeInt(pack, v.id or 0)
		DataPack.writeInt(pack, v.count or 0)
		DataPack.writeByte(pack, v.quality or 0)
		DataPack.writeByte(pack, v.bind or 0)
	end
	DataPack.flush(pack)
end

--根据排名获得活动奖励位置
function NightFightingGetAward(sysarg, idx)
	local fightingRewards = {}
	if(1 <= idx)then
		for k,v in pairs(NightFightingCfg.Awards)do
			if(v.condition[1] <= idx and idx <= v.condition[2])then
				fightingRewards = v.award
				break
			end
		end
	end
	local actorLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)	
	return fightingRewards --TransAwardsByType(fightingRewards, actorLevel)
end

--活动结束发奖
function NightFightingAward()
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local ranking = Ranking.getRanking( rankName )
	if (ranking == nil) then
		return
	end
	local num = NightFightingCfg.Awards[#NightFightingCfg.Awards].condition[2]
	local itemNum = Ranking.getRankItemCount(ranking)
	if itemNum > num then
		itemNum = num
	end	
	local title = Lang.ScriptTips.NightFighting005				
	local rankLimit = NightFightingCfg.rankLimit
	for idx=1,itemNum do
		local rankItem = Ranking.getItemFromIndex(ranking, idx-1)
		if rankItem then
			local actorId  = Ranking.getId(rankItem)
			local sysarg = Actor.getActorById(tonumber(actorId))
			local myScore = Ranking.getPoint(rankItem) or 0
			local content = string.format(Lang.ScriptTips.NightFighting006,idx)
			if(rankLimit <= myScore)then
				local ItemAwards = NightFightingGetAward(sysarg, idx)	
				if IsBagGridEnough(sysarg, ItemAwards) then
					GiveCommonAward(sysarg, ItemAwards, LogIds.NightFightingLogId, Lang.LogTips.NightFighting)
				else	
					Awards.GiveToMailById(actorId, ItemAwards, title, content, LogIds.NightFightingLogId, Lang.LogTips.NightFighting)
				end
			else
				break
			end
		end
	end	
end

--活动判断
function IsNightFighting(sysarg)
	local mySceneId =  Actor.getSceneId(sysarg)
	if(mySceneId == NightFightingCfg.EnterPos[1][1])then
		return true
	end
	return false
end

--新回合开始清除玩家阵营数据
function ClearNightFightingRankPlayerCamp()
	--print("ClearNightFightingRankPlayerCamp")
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local ranking = Ranking.getRanking( rankName )
	if (ranking ~= nil) then
		local rankCount = Ranking.getRankItemCount(ranking)
		for idx = 1, rankCount do
			local item = Ranking.getItemFromIndex( ranking, idx - 1)
			if item then
				Ranking.setSub(item, 3, 0) 
			end	
		end
	end	
end

--打开所有人的左侧面板
function OpenNightFightingRightPanelAll()
	local playerList = LuaHelp.getSceneActorListById( NightFightingCfg.EnterPos[1][1] ) or {}
	for i,v in ipairs(playerList) do
		OpenNightFightingRightPanel(v)
	end
end

--改变所有人的左侧面板
function ChangeNightFightingRightPanelAll(sysarg)
	local playerList = LuaHelp.getSceneActorListById( NightFightingCfg.EnterPos[1][1] ) or {}
	for i,v in ipairs(playerList) do
		if sysarg and sysarg == v then
		else
			ChangeNightFightingRightPanel(v)
		end
	end
end

--打开左侧面板
function OpenNightFightingRightPanel(sysarg)

	local panelInfo = {}
	
	panelInfo.sts = 1
	
	panelInfo.title = Lang.ScriptTips.NightFighting008

	local sysVar = System.getDyanmicVar()

	panelInfo.timeTitle = Lang.ScriptTips.NightFighting009
	panelInfo.restTime = sysVar.NightFightingStartTime + NightFightingCfg.Time - System.getCurrMiniTime()

	panelInfo.contentTitle = ""

	local myScore 	= 0
	local myIdx 	= 0
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local num = NightFightingCfg.RankCount
		local itemNum = Ranking.getRankItemCount(ranking)
		if num < Ranking.getRankItemCount(ranking) then
			itemNum = num
		end

		local rankLimit = NightFightingCfg.rankLimit
		local myId = Actor.getActorId(sysarg)
		local item = Ranking.getItemPtrFromId( ranking, myId)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < rankLimit then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end
	end
		
	

	panelInfo.contents = {}
	panelInfo.contents[1] = Lang.ScriptTips.NightFighting010..myScore
	panelInfo.contents[2] = Lang.ScriptTips.NightFighting011..myIdx
	
	panelInfo.awardTitle = ""
	panelInfo.awards = {}

	panelInfo.buttons = {1}

	local panelType = 2
	local activityId = enRightPanelActivity_NightFight
	OpenActivityRightPanel( sysarg, panelType, activityId, panelInfo )

end

--左侧面板改变
function ChangeNightFightingRightPanel(sysarg)

	local panelInfo = {}

	panelInfo.sts = 1

	panelInfo.contentTitle = ""

	local myScore 	= 0
	local myIdx 	= 0
	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local num = NightFightingCfg.RankCount
		local itemNum = Ranking.getRankItemCount(ranking)
		if num < Ranking.getRankItemCount(ranking) then
			itemNum = num
		end

		local rankLimit = NightFightingCfg.rankLimit
		local myId = Actor.getActorId(sysarg)
		local item = Ranking.getItemPtrFromId( ranking, myId)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < rankLimit then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end
	end
		
	

	panelInfo.contents = {}
	panelInfo.contents[1] = Lang.ScriptTips.NightFighting010..myScore
	panelInfo.contents[2] = Lang.ScriptTips.NightFighting011..myIdx
	

	panelInfo.buttons = {1}

	ChangeActivityRightPanel( sysarg, panelInfo )
end

--玩家随机方法
function GetNightFightingRandomNumber(nLen)
	local numTable = {}
	local stateTable = {}
	for idx = 1, nLen do
		local num = System.getRandomNumber(nLen) + 1
		while stateTable[num] do
			num = num + 1
			if num > nLen then
				num = num - nLen
			end
		end
		table.insert(numTable, num)
		stateTable[num] = 1
	end
	return numTable
end

function SendAchieveNightFightingRankEvent()

	local rankName = RankList[RANK_DEFINE_NIGHTFIGHTING].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local itemNum = Ranking.getRankItemCount(ranking)
		local rankLimit = NightFightingCfg.rankLimit
		for idx = 1, itemNum do
			local item = Ranking.getItemFromIndex( ranking, idx-1)
			if item then
				local myScore = Ranking.getPoint(item) or 0
				if myScore >= rankLimit then	
					local actorId = Ranking.getId(item)
					local sysarg = Actor.getActorById(actorId)		
					Actor.triggerAchieveEvent(sysarg, 44, 4, idx)
				else
					break
				end
			end
		end
	end
end

